provider "alicloud" {
  region                = local.region
  version               = "1.199"
}

terraform {
  required_version      = "~> 0.12"  
  backend "oss" {
    bucket              = "deeproute-terraform-resources"
    key                 = "stag/services/cloudbox-rds-example"
    tablestore_endpoint = "https://terraform-status.cn-shenzhen.ots.aliyuncs.com"
    tablestore_table    = "terraform_statelock_table"    
    region              = "cn-shenzhen"
  }
}

locals {
  region                = "cn-shenzhen"
  env                   = "stg"
  name                  = "cloudbox-rds-example"
  billing               = "team-cloud"
  team                  = "cloud"
}

locals {
  cloudbox_vpc                    = "vpc-wz94gvzjd0qnjti24e8is"
  public_cloudbox_vswitch         = "vsw-wz9itft4zwi2waedturj6"
  stg_cloudbox_vswitch            = "vsw-wz93tv3xo2ljpg4qppg4e"
  availability_zone               = "cn-shenzhen-cnszsg-cb"
}

module "ack_sg" {
  source                = "git@code.deeproute.ai:deeproute-org/cloud/cloud/k8s-deployment/ops-terraform.git//alicloud/sg?ref=module.v1.0.4"
  sg_type               = "enterprise"
  vpc_id                = local.cloudbox_vpc
  sg_name               = "${local.env}-${local.name}"
  sg_description        = "security group of RDS [${local.env}-${local.name}]"
  inbound_rules         = {
    "0" = ["10.246.0.0/16", "-1/-1", "all", "放行Ack集群VPC地址段"]
    "1" = ["0.0.0.0/0", "-1/-1", "icmp", "允许Ping"]
    "2" = ["10.24.0.0/22", "3306/3306", "tcp", "允许深圳办公网络对Apiserver访问"]
  }
}

module "rds" {
  source                      = "../../modules/alicloud/rds/rds-instance"
  rds_instance_name           = "${local.env}-${local.name}"
  rds_zone_id                 = "cn-shenzhen"
  rds_zone_id_slave_a         = local.availability_zone
  rds_vswitch_id              = [local.stg_cloudbox_vswitch]
  rds_security_group_ids      = [module.ack_sg.sg_id]
  rds_security_ips            = ["127.0.0.1"]
  rds_engine                  = "MySQL"
  rds_engine_version          = "8.0"
  rds_instance_type           = "mysql.x2.large.2c"  //https://help.aliyun.com/document_detail/26312.html?spm=a2c4g.11186623.6.570.37b93fa0e1d4lf
  rds_instance_storage        = "50"
  rds_instance_charge_type    = "Postpaid"
  rds_username                = "superadmin"
  rds_kms_password            = "MDBkM2FjZTItNjExMC00ZDVhLWJiNjktMTZkNTBhYzkxNTlk/H2cTERJTdzZSiwqha4VXK7TdU326L5oeZd2mOViw5B7wvqTYLbaQFHYfdIgRekGgg8aS9AWRHs="
  enable_rds_backup           = true
  rds_parameters              = [
    {
      name                    = "max_connect_errors"
      value                   = "100000"
    },    
    {
    name                      = "slow_launch_time"
    value                     = "1"
    },
  ]  
  tags = {
    Name                  = local.name
    Environment           = local.env
    Billing               = local.billing
    Team                  = local.team
  }    
}

module "rds_database" {
  source                      = "../../modules/alicloud/rds/rds-databases"
  rds_instance_id             = module.rds.rds_id
  rds_database_name           = "example"
}
